package com.alibaba.nacos.core.distributed.distro.task.execute;

import com.alibaba.nacos.common.task.AbstractExecuteTask;
import com.alibaba.nacos.core.distributed.distro.component.DistroCallback;
import com.alibaba.nacos.core.distributed.distro.component.DistroTransportAgent;
import com.alibaba.nacos.core.distributed.distro.entity.DistroKey;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;

@Setter
@Getter
@Accessors(chain = true)
public abstract class AbstractDistroExecuteTask
        extends AbstractExecuteTask {

    @Autowired
    private DistroTransportAgent distroTransportAgent;

    private DistroKey distroKey;

    @Override
    public void run() {
        if (distroTransportAgent.supportCallbackTransport()) {
            doExecuteWithCallback(new DistroExecuteCallback());
        } else {
            boolean b = doExecute();
            if (!b) {
                throw new UnsupportedOperationException();
            }
        }
    }

    protected abstract void doExecuteWithCallback(DistroCallback callback);

    protected abstract boolean doExecute();


    private class DistroExecuteCallback
            implements DistroCallback {

        @Override
        public void onSuccess() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void onFailed(Throwable throwable) {
            throw new UnsupportedOperationException();
        }
    }
}

